package com.chen.springcloud.demo.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 演示 uaa-provider 远程调用
 * @author chentian
 * @date 2021/12/24
 */
@Slf4j
@RestController
@RequestMapping("/api/call/uaa")
public class UaaCallController {

    /** 注入 Spring Boot 自动配置 RestTemplateBuilder 建造者 IOC 容器实例 */
    @Resource
    private RestTemplateBuilder restTemplateBuilder;

    @GetMapping("/user/hello/v1")
    public String remoteCallV1(){

        String url = "http://localhost:7702/uaa-provider/api/uaa/hello";

        log.info("url:" + url);

        // 使用建造者的 build() 方法建造 restTemplate 实例
        RestTemplate restTemplate = restTemplateBuilder.build();

        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);

        log.info("res:"+ responseEntity.getStatusCodeValue()+" body:"+responseEntity.getBody());
        return responseEntity.getBody();
    }


}
